package com.hnxxxy.sysmgr.mapper;

import com.hnxxxy.sysmgr.pojo.Staff;
import com.hnxxxy.sysmgr.vo.StaffVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StaffMapper {
    /**
     * 查询员工信息
     * @param staffVo
     * @return
     */
    @Select({"<script>",
            "select * from sys_staff ",
            "<where> ",
            "<if test='loginname!=null'>",
            "and loginname like concat('%',#{loginname},'%')",
            "</if>",
            "<if test='name!=null'>",
            "and name like concat('%',#{name},'%')",
            "</if>",
            "</where>",
            "</script>"})
    List<Staff> queryAllStaff(StaffVo staffVo);

    /**
     * 添加员工信息
     * @param staffVo
     */
    @Insert({"insert into sys_staff (name, loginname, pwd, email, phonenumber,department,position,createdate,salt)" +
            " values (#{name},#{loginname},#{pwd},#{email},#{phonenumber},#{department},#{position},#{createdate},#{salt})"})
    void insertStaff(StaffVo staffVo);


    /**
     * 更新员工信息
     * @param staffVo
     */
    @Update({"<script>",
            "update sys_staff",
            "<set>",
                "<if test='userid!=null'>",
                    " userid=#{userid},",
                "</if>",
                "<if test='name!=null'>",
                     "name=#{name},",
                "</if>",
                "<if test='loginname!=null'>",
                    " loginname=#{loginname},",
                "</if>",
                "<if test='pwd!=null'>",
                    "pwd=#{pwd},",
                "</if>",
                "<if test='email!=null'>",
                    "email=#{email},",
                "</if>",
                "<if test='phonenumber!=null'>",
                    "phonenumber=#{phonenumber},",
                "</if>",
                "<if test='department!=null'>",
                    "department=#{department},",
                "</if>",
                "<if test='position!=null'>",
                    "position=#{position},",
                "</if>",
                "<if test='createdate!=null'>",
                    "createdate=#{createdate},",
                "</if>",
                "<if test='salt!=null'>",
                    "salt=#{salt}",
                "</if>",
            "</set>",
            "where id=#{id}",
    "</script>"})
    int updateStffa(StaffVo staffVo);
    /**
     * 删除员工信息
     * @param id
     */
    @Delete({"delete from sys_staff where id = #{id}"})
    void deleteStaff(Integer id);

    /**
     * 保存用户和角色的关系
     * @param staffid
     * @param rid
     */
    @Insert({"insert into sys_staff_role (staffid,roleid) values (#{id},#{rid})"})
    void insertStaffRole(@Param("id") Integer staffid, @Param("rid") Integer rid);
}